package cn.caidabao.gb.component.sip.server.request.send;

import cn.caidabao.gb.bean.CallMessage;
import cn.caidabao.gb.bean.GBSipRegisterSession;
import cn.caidabao.gb.bean.InviteObject;
import cn.caidabao.gb.bean.OGSipCallIdCallBack;
import cn.caidabao.gb.component.sip.CustomSipServer;
import cn.caidabao.gb.component.sip.server.storage.CallBackStorage;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sip.message.Request;

@Component
public class InviteMessageSender extends RequestSender<InviteObject,Boolean>{
    @Resource
    CallBackStorage callBackStorage;
    @SneakyThrows
    @Override
    public OGSipCallIdCallBack<Boolean> sendAsync(GBSipRegisterSession gbSipRegisterSession, InviteObject extraData) {
        CallMessage callMessage = CustomSipServer.server.sendInviteMessage(gbSipRegisterSession.getDeviceId(),
                extraData.getChanelId(), gbSipRegisterSession.getIp()+":"+ gbSipRegisterSession.getPort(),
                extraData.getMediaIp(), extraData.getMediaPort(), Request.INVITE,extraData.getSdp());
        String callId = callMessage.getCallId();
        OGSipCallIdCallBack<Boolean> callBack = new OGSipCallIdCallBack(callId,extraData.getChanelId());
        callBack.setChannelId(extraData.getChanelId());
        callBack.setCallId(callId);
        callBackStorage.putCallBack(callId,callBack);
        callBack.setCSeq(callMessage.getCSeq());
        return callBack;
    }


}
